package com.lykj.modules.system.mapper;

import com.alibaba.excel.annotation.ExcelProperty;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lykj.modules.system.vo.UserVO;
import org.apache.ibatis.annotations.Param;
import com.lykj.modules.system.excel.UserExcel;
import com.lykj.modules.system.entity.User;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * Mapper 接口
 *
 * @author cxl
 */
public interface UserMapper extends BaseMapper<User> {

	/**
	 * 自定义分页
	 *
	 * @param page
	 * @param user
	 * @return
	 */
	List<UserVO> selectUser(IPage page, UserVO user);

	/**
	 * 自定义分页
	 *
	 * @param page
	 * @param user
	 * @param deptIdList
	 * @param tenantId
	 * @return
	 */
	List<User> selectUserPage(IPage<User> page, @Param("user") User user, @Param("deptIdList") List<Long> deptIdList, @Param("tenantId") String tenantId);

	/**
	 * 获取用户
	 *
	 * @param tenantId
	 * @param account
	 * @param password
	 * @return
	 */
	User getUser(String tenantId, String account, String password);

	/**
	 * 获取用户
	 *
	 * @param tenantId
	 * @param account
	 * @return
	 */
	User getUserInfo(String tenantId, String account);

	/**
	 * 获取导出用户数据
	 *
	 * @param queryWrapper
	 * @return
	 */
	List<UserExcel> exportUser(@Param("ew") Wrapper<User> queryWrapper);


	@Update("update `lykj_user` set day_withdrawal_limit = #{dayWithdrawalLimit} where id in(${userIds})")
	int updateDayWithdrawalLimit(@Param("userIds") String userIds, @Param("dayWithdrawalLimit") String dayWithdrawalLimit);
	@Update("update `lykj_user` set sum_withdrawal_limit = #{dayWithdrawalLimit} where id in(${userIds})")
	int updateSumWithdrawalLimit(@Param("userIds") String userIds, @Param("dayWithdrawalLimit") String dayWithdrawalLimit);
	@Update("update `lykj_user` set bonus_status = #{bonusStatus} where id in(${userIds})")
	int updateBonusStatus(@Param("userIds") String userIds, @Param("bonusStatus") String bonusStatus);
	/**
	 * 懒加载用户列表
	 *
	 * @param tenantId
	 * @param parentId
	 * @param param
	 * @return
	 */
	List<UserVO> lazyList(String tenantId, Long parentId, Map<String, Object> param);

	/**
	 * 增加余额
	 *
	 * @param number
	 * @param id
	 */
	@Update("update `lykj_user` set sys_balance = sys_balance+#{number} where id = #{id}")
	void inBalance(@Param("number") BigDecimal number, @Param("id") Long id);



	/**
	 * 增加余额
	 *
	 * @param number
	 * @param id
	 */
	@Update("update `lykj_user` set earmarked_money = earmarked_money+#{number} where id = #{id}")
	void inEarmarkedMoney(@Param("number") BigDecimal number, @Param("id") Long id);

	/**
	 * 扣除余额
	 *
	 * @param number
	 * @param id
	 */
	@Update("update `lykj_user` set earmarked_money = earmarked_money-#{number} where id = #{id}")
	void decEarmarkedMoney(@Param("number") BigDecimal number, @Param("id") Long id);



	/**
	 * 扣除余额
	 *
	 * @param number
	 * @param id
	 */
	@Update("update `lykj_user` set sys_balance = sys_balance-#{number} where id = #{id}")
	void decBalance(@Param("number") BigDecimal number, @Param("id") Long id);

	/**
	 * 增加积分
	 *
	 * @param number
	 * @param id
	 */
	@Update("update `lykj_user` set sys_integral = sys_integral+#{number} where id = #{id}")
	void inIntegral(@Param("number") BigDecimal number, @Param("id") Long id);

	/**
	 * 扣除积分
	 *
	 * @param number
	 * @param id
	 */
	@Update("update `lykj_user` set sys_integral = sys_integral-#{number} where id = #{id}")
	void decIntegral(@Param("number") BigDecimal number, @Param("id") Long id);

	/**
	 * 增加贡献值
	 *
	 * @param number
	 * @param id
	 */
	@Update("update `lykj_user` set sys_contribute = sys_contribute+#{number} where id = #{id}")
	void inContribute(@Param("number") BigDecimal number, @Param("id") Long id);

	/**
	 * 扣除贡献值
	 *
	 * @param number
	 * @param id
	 */
	@Update("update `lykj_user` set sys_contribute = sys_contribute-#{number} where id = #{id}")
	void decContribute(@Param("number") BigDecimal number, @Param("id") Long id);

//	/**
//	 * 累计订单总额
//	 *
//	 * @param number
//	 * @param id
//	 */
//	@Update("update `lykj_user` set orders_total = orders_total+#{number} where id = #{id}")
//	void inOrdersTotal(@Param("number") BigDecimal number, @Param("id") Long id);
//
//	/**
//	 * 累计分红订单总额
//	 *
//	 * @param number
//	 * @param id
//	 */
//	@Update("update `lykj_user` set bonus_orders = bonus_orders+#{number} where id = #{id}")
//	void inBonusOrders(@Param("number") BigDecimal number, @Param("id") Long id);
//
//	/**
//	 * 累计领取分红
//	 *
//	 * @param number
//	 * @param id
//	 */
//	@Update("update `lykj_user` set sys_balance = sys_balance+#{number},bonus_total = bonus_total+#{number} where id = #{id}")
//	void inBonusTotal(@Param("number") BigDecimal number, @Param("id") Long id);

	/**
	 * 累计收益
	 *
	 * @param number
	 * @param id
	 */
	@Update("update `lykj_user` set sys_balance = sys_balance+#{number},earnings_total = earnings_total+#{number} where id = #{id}")
	void inEarningsTotal(@Param("number") BigDecimal number, @Param("id") Long id);

	/**
	 * 贡献值兑换余额
	 *
	 * @param number
	 * @param id
	 */
	@Update("update `lykj_user` set sys_contribute = sys_contribute-#{contribute},sys_balance = sys_balance+#{number} where id = #{id}")
	void transferContribute(@Param("contribute") BigDecimal contribute, @Param("number") BigDecimal number, @Param("id") Long id);

	/**
	 * 分配分红资格
	 *
	 * @param status
	 * @param id
	 */
	@Update("update `lykj_user` set bonus_status = #{status} where id = #{id}")
	void bonusStatus(@Param("status") Integer status, @Param("id") Long id);


	@Update("update `lykj_user` set bonus_status = #{status}")
	void allBonusStatus(@Param("status") Integer status);

	/**
	 * 会有升级
	 *
	 * @param id
	 */
	@Update("update `lykj_user` set user_sign = user_sign+1 where id = #{id}")
	void upgrade(@Param("id") Long id);

	@Select("<script>select u.id,uid,u.account,u.name,u.spread_id,u.sys_integral,u.sys_balance,u.sys_contribute,u.bonus_status,u.user_sign,u.zn_user_id,u.zn_balance,u.create_time,u.earnings_total," +
		"(select if(sum(o.total_price) is null,0,sum(o.total_price)) from shsy_user_order o where o.user_id=u.id and o.pay_type in(0,2) and o.`status`=1 and o.is_deleted=0) orders_total," +
		"(select if(sum(o.pv_total_price) is null,0,sum(o.pv_total_price)) from shsy_user_order o where o.user_id=u.id and o.pay_type in(0,2) and o.`status`=1 and o.is_deleted=0) bonus_orders," +
		"(select if(sum(b.number) is null,0,sum(b.number)) from shsy_user_bonus b where b.user_id=u.id and b.`status`=1 and b.is_deleted=0) bonus_total" +
		" from lykj_user u where u.is_deleted = 0" +
		"<if test=\"user.tenantId!=null and user.tenantId != ''\">" +
		" and u.tenant_id = #{user.tenantId}" +
		"</if>" +
		"<if test=\"user.id!=null\">" +
		" and u.id = #{user.id}" +
		"</if>" +
		"<if test=\"user.account!=null and user.account != ''\">" +
		" and u.account = #{user.account}" +
		"</if>" +
		"<if test=\"user.realName!=null and user.realName != ''\">" +
		" and u.real_name = #{user.realName}" +
		"</if>" +
		"<if test=\"user.userType!=null and user.userType != ''\">" +
		" and u.user_type = #{user.userType}" +
		"</if>" +
		"<if test=\"user.userSign!=null\">" +
		" and u.user_sign = #{user.userSign}" +
		"</if>" +
		" ORDER BY u.id" +
		"</script>")
	List<com.lykj.modules.shop.excel.UserExcel> export(@Param("user") User user);
}
